package com.my.study.filter;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.safety.Whitelist;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.util.StreamUtils;
import org.springframework.util.StringUtils;

import javax.servlet.ReadListener;
import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;

/**
 * @author: yidujun
 * @create: 2021/04/16 15:02
 */
public class XssHttpServletRequestWrapper extends HttpServletRequestWrapper {


    private static final Whitelist JSOUP_WHITE_LIST = Whitelist.basic();
    private static final Document.OutputSettings JSOUP_OUTPUT_SETTINGS = new Document.OutputSettings();


    static {
        JSOUP_WHITE_LIST.removeTags("a");
        JSOUP_OUTPUT_SETTINGS.prettyPrint(false);
    }


    public XssHttpServletRequestWrapper(HttpServletRequest request) {
        super(request);
    }

    @Override
    public String[] getParameterValues(String name) {
        String[] values = super.getParameterValues(name);
        if (null != values) {
            int length = values.length;
            String[] escapseValues = new String[length];
            for (int i = 0; i < length; i++) {
                escapseValues[i] = stripXSS(values[i]);
            }
            return escapseValues;
        }

        return super.getParameterValues(name);
    }

    @Override
    public String getParameter(String name) {
        String values = super.getParameter(name);
        return stripXSS(values);
    }

    @Override
    public String getHeader(String name) {
        String values = super.getHeader(name);
        return stripXSS(values);
    }

    @Override
    public ServletInputStream getInputStream() throws IOException {
        if (!isJsonRequest()) {
            return super.getInputStream();
        }

        String json = StreamUtils.copyToString(super.getInputStream(), StandardCharsets.UTF_8);
        if (!StringUtils.isEmpty(json)) {
            return super.getInputStream();
        }

        //XSS过滤
        json = stripXSS(json);
        final ByteArrayInputStream bis = new ByteArrayInputStream(json.getBytes(StandardCharsets.UTF_8));
        return new ServletInputStream() {
            @Override
            public boolean isFinished() {
                return false;
            }

            @Override
            public boolean isReady() {
                return false;
            }

            @Override
            public void setReadListener(ReadListener readListener) {

            }

            @Override
            public int read() throws IOException {
                return bis.read();
            }
        };
    }

    /**
     * XSS过滤
     *
     * @param value
     * @return
     */
    private String stripXSS(String value) {
        if (null != value) {
            value = value.replaceAll("", "");
            value = Jsoup.clean(value, "", JSOUP_WHITE_LIST, JSOUP_OUTPUT_SETTINGS);
        }
        return value;
    }

    /**
     * 是否是Json请求
     */
    public boolean isJsonRequest() {
            String header = super.getHeader(HttpHeaders.CONTENT_TYPE);
        return MediaType.APPLICATION_JSON_VALUE.equalsIgnoreCase(header)
                || MediaType.APPLICATION_JSON_UTF8_VALUE.equalsIgnoreCase(header);
    }

}
